<?php

class LangAction extends PublicAction {

    protected  $langpath,$lang;

    function _initialize()
    {
        parent::_initialize();
        $this->langpath = LANG_PATH.LANG_NAME.'/';
    }

    public function index()
    {
        $model = M('Lang');
        if(empty($_REQUEST['where'])){
            $list = $model->select();
        }else{
            $list = $model->where($_REQUEST['where'])->select();
        }

        $this->assign('list', $list);
        $this->display();
    }
    function edit()
    {

        $name = MODULE_NAME;
        $model = M($name);
        $pk = ucfirst($model->getPk());
        $id = $_REQUEST[$model->getPk()];

        if(empty($id))
            $this->error(L('do_empty'));

        $do = 'getBy'.$pk;
        $vo = $model->$do( $id );

        if($vo['setup'])
            $vo['setup'] = string2array($vo['setup']);

        $this->assign('vo', $vo);
        $this->display();
    }
    function _before_insert()
    {
        $lang_path = LANG_PATH.$_POST['mark'].'/';
        $r = dir_copy(LANG_PATH.'cn/',$lang_path);
    }
    function param()
    {
        $files = glob($this->langpath.'*');
        $lang_files = array();
        foreach($files as $key => $file) {
            //$filename = basename($file);
            $filename = pathinfo($file);
            $lang_files[$key]['filename'] = $filename['filename'];
            $lang_files[$key]['filepath'] = $file;
            $temp = explode('_',$lang_files[$key]['filename']);
            $lang_files[$key]['name'] = count($temp)>1 ? $temp[0].L('LANG_module') : L('LANG_common') ;
        }
        $this->assign('id', $id);
        $this->assign('lang', LANG_NAME);
        $this->assign('files', $lang_files);
        $this->display();
    }

    function editparam()
    {
        $file = $_REQUEST['file'];
        $value = F($file, $value='', $this->langpath);
        $this->assign('id', $id);
        $this->assign('file', $file);
        $this->assign('lang', LANG_NAME);
        $this->assign('list', $value);
        $this->display();
    }

    function updateparam()
    {
        $file = $_REQUEST['file'];
        unset($_POST[C('TOKEN_NAME')]);

        foreach($_POST as $key=>$r){
            if($r)$data[strtoupper($key)]=$r;
        }
        $r = F($file,$data, $this->langpath);
        if($r){
            $this->success(L('do_ok'));
        }else{
            $this->error(L('add_error'));
        }
    }

    function update()
    {

        if($_POST['setup'])
            $_POST['setup'] = array2string($_POST['setup']);

        $name = MODULE_NAME;
        $model = D( $name );

        if (false === $model->create()) {
          $this->error($model->getError ());
        }

        if (false !== $model->save ()) {

            if(in_array(ucfirst($name),$this->cache_model))
                savecache($name);

            if($_POST['aid']){
                $Attachment =M('attachment');
                $aids =  implode(',',$_POST['aid']);
                $data['id'] = $_POST['id'];
                $data['catid'] = intval($_POST['catid']);
                $data['status'] = '1';
                $Attachment->where("aid in (".$aids.")")->save($data);
            }

            if($_POST['isajax'])
                $this->assign('dialog','1');

            $jumpUrl = $_POST['forward'] ? $_POST['forward'] : U(MODULE_NAME.'/index');

            $this->assign('jumpUrl', $jumpUrl);
            $this->success(L('edit_ok'));
        } else {
            $this->success (L('edit_error').': '.$model->getDbError());
        }
    }

    function add()
    {
        $this->display('edit');
    }

}